package com.zhouyibike.uu.ui.activity.xingcheng;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.MyJourneyResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.listener.MyListener;
import com.zhouyibike.uu.ui.adapter.MyJourneyAdapter;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.ui.view.PullToRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyJourneyActivity extends BaseActivity implements View.OnClickListener {
    private ImageButton mBtnBack;
    private TextView mBtnTitle;
    private PullToRefreshLayout pullToRefreshLayout;
    private int refreshtype = 0;
    private int page = 1;//页码
    private boolean ismore = true;//判断是否还有更多数据
    private LinearLayout ll_nothing;
    private MyJourneyAdapter myJourneyAdapter;
    private List<MyJourneyResult.DataBean> alllist = new ArrayList<>();
    private ListView lv_allmsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_journey);
        initView();
        getMsg();
    }

    private void initView() {
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("我的行程");
        mBtnBack.setOnClickListener(this);

        pullToRefreshLayout = ((PullToRefreshLayout) findViewById(R.id.refresh_view));
        pullToRefreshLayout.setOnRefreshListener(new MyListener2());
        ll_nothing = (LinearLayout) findViewById(R.id.ll_nothing_show);
        lv_allmsg = (ListView) findViewById(R.id.content_view);
    }

    class MyListener2 extends MyListener {
        @Override
        public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
            super.onRefresh(pullToRefreshLayout);
            refreshtype = 1;
            page = 1;
            ismore = true;
            getMsg();
        }

        @Override
        public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
            super.onLoadMore(pullToRefreshLayout);
            refreshtype = 2;
            if (ismore) {
                page++;
                getMsg();
            } else {
                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }
        }
    }

    private void getMsg() {
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token", ""));
        params.put("pageIndex", page + "");
        if (page == 1 && refreshtype == 0)
            showProgressDialog();
        addSubscription(apiStores.MyJourney(params),
                new ApiCallback<MyJourneyResult>() {

                    @Override
                    public void onSuccess(MyJourneyResult model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {
                            if (refreshtype == 1) {
                                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                                alllist.clear();
                            } else if (refreshtype == 2) {
                                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                            }
                            if (model.getData().size() == 0) {
                                ismore = false;
                            }
                            alllist.addAll(model.getData());
                            if (alllist.size() == 0) {
                                ll_nothing.setVisibility(View.VISIBLE);
                                pullToRefreshLayout.setVisibility(View.GONE);
                            } else {
                                ll_nothing.setVisibility(View.GONE);
                                pullToRefreshLayout.setVisibility(View.VISIBLE);
                                initData();
                            }

                        } else if (model.getStatus() == 508 || model.getStatus() == 506 || model.getStatus() == 307) {
                            setResult(2501);
                            finish();
                        } else {
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);

                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }
                });
    }

    private void initData() {
        if (page == 1) {
            myJourneyAdapter = new MyJourneyAdapter(this, alllist);
            lv_allmsg.setAdapter(myJourneyAdapter);
        } else {
            myJourneyAdapter.setList(alllist);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bar_btn_back:
                finish();
                break;

        }
    }
}
